package com.childenglish.mapper;

import com.childenglish.entity.Notification;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface NotificationMapper {

    @Insert("INSERT INTO notification (user_id, title, content, type, read_status, create_time, related_id, related_type) " +
            "VALUES (#{userId}, #{title}, #{content}, #{type}, #{readStatus}, #{createTime}, #{relatedId}, #{relatedType})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Notification notification);

    @Select("SELECT * FROM notification WHERE user_id = #{userId} ORDER BY create_time DESC")
    @Results(id = "notificationMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "content", column = "content"),
            @Result(property = "type", column = "type"),
            @Result(property = "readStatus", column = "read_status"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "readTime", column = "read_time"),
            @Result(property = "relatedId", column = "related_id"),
            @Result(property = "relatedType", column = "related_type")
    })
    List<Notification> selectByUserId(Long userId);

    @Select("SELECT * FROM notification WHERE user_id = #{userId} AND read_status = 0 ORDER BY create_time DESC")
    @ResultMap("notificationMap")
    List<Notification> selectUnreadByUserId(Long userId);

    @Update("UPDATE notification SET read_status = 1, read_time = NOW() WHERE id = #{id}")
    int markAsRead(Long id);

    @Update("UPDATE notification SET read_status = 1, read_time = NOW() WHERE user_id = #{userId}")
    int markAllAsRead(Long userId);

    @Select("SELECT COUNT(*) FROM notification WHERE user_id = #{userId} AND read_status = 0")
    int countUnread(Long userId);
}

